/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMCategoriesSettings;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;
import Storage.*;
import VocabularyManagement.EditVocDialog;

/**
 * @author Christoph Schnack
 *
 */
public class ApplyChangesDialog {
	Settings SET;
	Language LANG;
	Storage MAINVOC;
	Mainframe MAINFRAME;
	Vocabulary vocToEdit;
	MDManager VMDM;
	Vector<String> newA,newB;
	boolean differenceA,differenceB;
	MDTribuitFrame frame,parentFrame;
	HashMap<String, JComboBox> hmA,hmB;
	
	public ApplyChangesDialog(Mainframe mf, Storage mv, Settings set, MDManager vmdm, MDTribuitFrame parentArg, Vocabulary vte, Vector<String> nA, boolean diffA, Vector<String> nB,boolean diffB){
		MAINFRAME = mf;
		MAINVOC = mv;
		SET = set;
		LANG = set.getLanguage();
		VMDM = vmdm;
		vocToEdit = vte;
		parentFrame = parentArg;
		newA = nA;
		differenceA = diffA;
		newB = nB;
		differenceB = diffB;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				buildGUI();
			}
		});
	}
	
	public void buildGUI(){
		frame = new MDTribuitFrame(LANG.getApplyChanges(),VMDM);
		
		JTabbedPane jtb = new JTabbedPane();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		++gbc.gridy;
		if(differenceA){
			hmA = new HashMap<String, JComboBox>();
			JPanel panelA = new JPanel();
			panelA.setLayout(new GridBagLayout());
			JComboBox coBo;
			JLabel label;
			for(String s : vocToEdit.getCategoriesA()){
				gbc.gridx = 1;
				coBo = new JComboBox(newA);
				label = new JLabel(s);
				panelA.add(label,gbc);
				gbc.gridx = 2;
				panelA.add(new JLabel("->"),gbc);
				gbc.gridx = 3;
				panelA.add(coBo,gbc);
				hmA.put(s,coBo);
				gbc.gridy +=1;
				coBo.setSelectedItem(s);
			}
			CustomScrollPane csp = new CustomScrollPane(panelA,150,200);
			csp.setPreferredSize(new Dimension(150,200));
			jtb.addTab(vocToEdit.getNameA(), csp);
		}
		if(differenceB){
			hmB = new HashMap<String, JComboBox>();
			JPanel panelB = new JPanel();
			panelB.setLayout(new GridBagLayout());
			JComboBox coBo;
			JLabel label;
			gbc.gridy = 0;
			++gbc.gridy;
			for(String s : vocToEdit.getCategoriesB()){
				gbc.gridx = 1;
				coBo = new JComboBox(newB);
				label = new JLabel(s);
				panelB.add(label,gbc);
				gbc.gridx = 2;
				panelB.add(new JLabel("->"),gbc);
				gbc.gridx = 3;
				panelB.add(coBo,gbc);
				hmB.put(s,coBo);
				gbc.gridy +=1;
				coBo.setSelectedItem(s);
			}
			CustomScrollPane csp = new CustomScrollPane(panelB);
			csp.setPreferredSize(new Dimension(150,200));
			jtb.addTab(vocToEdit.getNameB(), csp);
		}
		
		
		JButton applyBtn = new JButton(LANG.getApply());
		applyBtn.addActionListener(new ApplyBtnListener());
		JButton backBtn = new JButton(LANG.getBack());
		backBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				frame.dispose();
				VMDM.setCurrentDialog(parentFrame);
				parentFrame.setVisible(true);
			}
		});
		JButton cancelBtn = new JButton(LANG.getDiscardAndCancel());
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				frame.dispose();
				parentFrame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		buttonPanel.add(applyBtn);
		buttonPanel.add(backBtn);
		buttonPanel.add(cancelBtn);
		
		frame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getApplyChanges()));
		frame.getContentPane().add(BorderLayout.CENTER,jtb);
		frame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		frame.showUpAt(MAINFRAME);
	}
	
	class ApplyBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(differenceA){
				for(Word w : vocToEdit.getWords()){
					for(Meaning m : w.getMeaningVectorA()){
						m.setCategory((String)hmA.get(m.getCategory()).getSelectedItem());
						if(m.hasSynonyms()){
							for(Term t : m.getSynonyms()){
								t.setCategory((String)hmA.get(t.getCategory()).getSelectedItem());
							}
						}
					}
				}
				vocToEdit.setCategoriesA(newA);
			}
			if(differenceB){
				for(Word w : vocToEdit.getWords()){
					for(Meaning m : w.getMeaningVectorB()){
						m.setCategory((String)hmB.get(m.getCategory()).getSelectedItem());
						if(m.hasSynonyms()){
							for(Term t : m.getSynonyms()){
								t.setCategory((String)hmB.get(t.getCategory()).getSelectedItem());
							}
						}
					}
				}
				vocToEdit.setCategoriesB(newB);
			}
			frame.dispose();
			MAINVOC.saveVocabularyIfAutosave(vocToEdit);
			new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
		}
	}
}
